package com.lzl.artisan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzl.artisan.model.Comment;
import com.lzl.artisan.model.Result;
import com.lzl.artisan.model.dto.CommonDto;
import com.lzl.artisan.service.CommentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController{
    @Autowired
    private CommentService commentService;

    @ApiOperation("根据文章Id查询评论")
    @GetMapping("listByPassageId/{passageId}")
    public Result listByPassageId(@PathVariable Long passageId){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("passage_id",passageId);

        Page<Comment> page = commentService.page(getPage(), wrapper);
        return Result.ok(page);
    }

    @ApiOperation("用户进行评论")
    @PostMapping("save")
    public Result save(@RequestBody CommonDto commonDto){
        Comment comment = new Comment(null,getUserId(),commonDto.getPassageId(),commonDto.getContent(),new Date(),new Date(),0);
        boolean save = commentService.save(comment);
        return save?Result.ok():Result.fail("评论失败");
    }
}
